package simulatorGUI;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.SwingUtilities;

import everything.Agent;
import everything.WorldMap;



/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class Simulator extends javax.swing.JFrame {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JPanel jPanel1;
	private JButton RunButton;
	private JButton quitButton1;
	private JTextField movesField;
	private JLabel moves;
	private JLabel lblPicture;
	private JLabel jLabel1;
	private JTextField txtYLocation;
	private JTextField txtXLocation;
	private JLabel posLabel2;
	private JComboBox colorCombo;
	private JLabel colorjLabel2;
	private JComboBox cmbDirection;
	private JTextField txtName;
	private JLabel lblRemember;
	private JCheckBox chkRemember;
	private JLabel NamejLabel1;
	private JPanel jPanel3;
	private JTextField FilejTextField2;
	private JLabel mapjLabel2;
	private JLabel lbldirection;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Simulator inst = new Simulator();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
//		Thread t1 = new Thread(new Runnable(){
//			public void run(){
//				try {
//					Mymain.main(new String[]{"Mymain"});
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//		});
//		t1.start();
	}

	public Simulator() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().add(getJPanel1(), BorderLayout.CENTER);
			pack();
			this.setSize(458, 296);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setPreferredSize(new java.awt.Dimension(442, 519));
			jPanel1.setLayout(null);
			jPanel1.setBorder(new LineBorder(new java.awt.Color(0,0,0),1,false));
			jPanel1.add(getRunButton());
			jPanel1.add(getQuitButton1());
			jPanel1.add(getMapjLabel2());
			jPanel1.add(getFilejTextField2());
			jPanel1.add(getJPanel3());
			jPanel1.add(getJLabel1());
			jPanel1.add(getMoves());
			jPanel1.add(getMovesField());
			jPanel1.add(getChkRemember());
			jPanel1.add(getLblRemember());
		}
		return jPanel1;
	}
	private JButton getRunButton() {
		if(RunButton == null) {
			RunButton = new JButton();
			RunButton.setText("RUN");
			RunButton.setBounds(97, 214, 91, 21);
			RunButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					File f1 = new File(FilejTextField2.getText()+".txt");
					File f2 = new File(FilejTextField2.getText()+".ini");
					if (f1.exists() && f2.exists()){
						new Thread(new Runnable(){
							// run program
							public void run(){
								try{
									int moves =  Integer.parseInt(movesField.getText());

									Agent e = new Agent(txtName.getText(), (String)(colorCombo.getSelectedItem()), Integer.parseInt(txtXLocation.getText()), Integer.parseInt(txtYLocation.getText()), 0) {
										@Override
										public void makeMove(WorldMap _map) {
											// TODO Auto-generated method stub
										}
									};
									
									WorldMap m = new WorldMap(FilejTextField2.getText(), moves, chkRemember.isSelected());
									m.runWorld(e);
								}catch (NumberFormatException e){
									e.printStackTrace();
									JOptionPane.showMessageDialog(null, "Invalid location - not an integer", "Error", JOptionPane.ERROR_MESSAGE);
									System.exit(0);
								}
							}
						}).start();
						dispose();
					} else {
						JOptionPane.showMessageDialog(null, "Invalid file (.txt or .ini) are missing");
					}
					//setVisible(false);
				}
			});
		}
		return RunButton;
	}
	private JButton getQuitButton1() {
		if(quitButton1 == null) {
			quitButton1 = new JButton();
			quitButton1.setText("Exit");
			quitButton1.setBounds(267, 214, 77, 21);
			quitButton1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					//System.out.println("quitButton1.actionPerformed, event="+evt);
					System.exit(0);
				}
			});
		}
		return quitButton1;
	}
	private JLabel getMapjLabel2() {
		if(mapjLabel2 == null) {
			mapjLabel2 = new JLabel();
			mapjLabel2.setText("Level:");
			mapjLabel2.setBounds(38, 185, 29, 14);
		}
		return mapjLabel2;
	}
	private JTextField getFilejTextField2() {
		if(FilejTextField2 == null) {
			FilejTextField2 = new JTextField();
			FilejTextField2.setText("board5");
			FilejTextField2.setBounds(73, 182, 240, 21);
		}
		return FilejTextField2;
	}
	private JPanel getJPanel3() {
		if(jPanel3 == null) {
			jPanel3 = new JPanel();
			jPanel3.setLayout(null);
			jPanel3.setBounds(38, 31, 370, 109);
			jPanel3.setEnabled(false);
			jPanel3.setBorder(new LineBorder(new java.awt.Color(0,0,0),1,false));
			jPanel3.add(getNamejLabel1());
			jPanel3.add(getJTextField1());
			jPanel3.add(getLblPicture());
			jPanel3.add(getDirection());
			jPanel3.add(getDirectoin());
			jPanel3.add(getColorjLabel2());
			jPanel3.add(getColorCombo());
			jPanel3.add(getPosLabel2());
			jPanel3.add(getXjTextField2());
			jPanel3.add(getYjTextField2());
		}
		return jPanel3;
	}
	private JLabel getNamejLabel1() {
		if(NamejLabel1 == null) {
			NamejLabel1 = new JLabel();
			NamejLabel1.setText("Name");
			NamejLabel1.setBounds(7, 5, 37, 26);
		}
		return NamejLabel1;
	}
	private JTextField getJTextField1() {
		if (txtName == null) {
			txtName = new JTextField();
			jPanel1.add(getJPanel3());
			//jPanel1.add(getName());
			txtName.setBounds(164, 8, 101, 21);
			txtName.setText("Tal");
		}
		return txtName;
	}
	private JLabel getDirection() {
		if(lbldirection == null) {
			lbldirection = new JLabel();
			lbldirection.setText("Starting Direction");
			lbldirection.setBounds(7, 59, 83, 14);
		}
		return lbldirection;
	}
	private JComboBox getDirectoin() {
		if(cmbDirection == null) {
			ComboBoxModel directoinModel =
				new DefaultComboBoxModel(
						new String[] { "North", "South","West","East" });
			cmbDirection = new JComboBox();
			cmbDirection.setModel(directoinModel);
			cmbDirection.setBounds(164, 56, 76, 21);
		}
		return cmbDirection;
	}
	private JLabel getColorjLabel2() {
		if(colorjLabel2 == null) {
			colorjLabel2 = new JLabel();
			colorjLabel2.setText("Color");
			colorjLabel2.setBounds(7, 35, 25, 14);
		}
		return colorjLabel2;
	}
	private JComboBox getColorCombo() {
		if(colorCombo == null) {
			ComboBoxModel colorComboModel =
				new DefaultComboBoxModel(
						new String[] { "Blue", "Red" });
			colorCombo = new JComboBox();
			colorCombo.setModel(colorComboModel);
			colorCombo.setBounds(164, 32, 74, 21);
			lblPicture.setIcon(getImage("images/AgentNorth.jpg"));
			colorCombo.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					if ("Blue".equals(colorCombo.getSelectedItem())){
						lblPicture.setIcon(getImage("images/AgentNorth.jpg"));
					} else if ("Red".equals(colorCombo.getSelectedItem())){
						lblPicture.setIcon(getImage("images/AgentNorthRed.jpg"));
					}
				}
			});
		}
		return colorCombo;
	}
	private JLabel getPosLabel2() {
		if(posLabel2 == null) {
			posLabel2 = new JLabel();
			posLabel2.setText("Starting Position");
			posLabel2.setBounds(7, 83, 78, 14);
		}
		return posLabel2;
	}
	private JTextField getXjTextField2() {
		if(txtXLocation == null) {
			txtXLocation = new JTextField();
			txtXLocation.setText("3");
			txtXLocation.setBounds(164, 80, 20, 21);
		}
		return txtXLocation;
	}
	private JTextField getYjTextField2() {
		if(txtYLocation == null) {
			txtYLocation = new JTextField();
			txtYLocation.setText("5");
			txtYLocation.setBounds(189, 80, 20, 21);
		}
		return txtYLocation;
	}

	private JLabel getJLabel1() {
		if(jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("Agents");
			jLabel1.setBounds(38, 12, 34, 14);
		}
		return jLabel1;
	}

	private JLabel getLblPicture() {
		if(lblPicture == null) {
			lblPicture = new JLabel();
			lblPicture.setBounds(301, 11, 52, 43);
		}
		return lblPicture;
	}

	/*
	private JLabel getName() {
		if(name == null) {
			name = new JLabel();
			name.setText("name");
			name.setBounds(146, 120, 372, 189);
		}
		return name;
	}

	private JLabel getNamex() {
		if(name == null)		return name;
	}
	 */

	private ImageIcon getImage(String path){
		URL url = getClass().getResource("/" + path);
		if (url == null){
			return new ImageIcon(path);
		} else {
			try {
				return new ImageIcon(ImageIO.read(url));
			} catch (IOException e) {
				return new ImageIcon(path);
			}
		}
	}

	private JLabel getMoves() {
		if(moves == null) {
			moves = new JLabel();
			moves.setText("Moves:");
			moves.setBounds(38, 154, 56, 14);
		}
		return moves;
	}

	private JTextField getMovesField() {
		if(movesField == null) {
			movesField = new JTextField();
			movesField.setText("5");
			movesField.setBounds(107, 151, 36, 20);
		}
		return movesField;
	}
	
	private JCheckBox getChkRemember() {
		if(chkRemember == null) {
			chkRemember = new JCheckBox();
			chkRemember.setBounds(339, 154, 18, 14);
			chkRemember.setSelected(true);
		}
		return chkRemember;
	}
	
	private JLabel getLblRemember() {
		if(lblRemember == null) {
			lblRemember = new JLabel();
			lblRemember.setText("Remember markov states?");
			lblRemember.setBounds(198, 154, 127, 14);
		}
		return lblRemember;
	}

	//	private JScrollPane getTblProbabilities() {
	//		if(tblProbabilities == null) {
	//
	//			String[][] Regions = new String[3][7];
	//			TableModel tblProbabilitiesModel = 
	//				new DefaultTableModel(
	//						new String[][] { { "one", "two", "three", "four", "five", "six" }, { "seven", "eight", "nine", "ten", "eleven", "twelve" } },
	//						new String[] { "Region", "SWarm", "SCold", "SFrigid", "WWarm", "WCold", "WFrigid" });
	//			tblProbabilities = new JTable();
	//			tblProbabilities.setModel(tblProbabilitiesModel);
	//			//tblProbabilities.setBounds(38, 159, 370, 83);
	//			pnlProbabilities = new JScrollPane(tblProbabilities);
	//			pnlProbabilities.setBounds(38, 159, 370, 83);
	//		}
	//		return pnlProbabilities;
	//	}

}
